package com.chinathinks.sms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinathinks.core.json.JsonHelper;
import com.chinathinks.core.metatype.Dto;
import com.chinathinks.core.metatype.impl.BaseDto;
import com.chinathinks.core.model.dao.Dao;
import com.chinathinks.core.mvc.xstruts.action.ActionForm;
import com.chinathinks.core.mvc.xstruts.action.ActionForward;
import com.chinathinks.core.mvc.xstruts.action.ActionMapping;
import com.chinathinks.core.util.G4Constants;
import com.chinathinks.core.web.BizAction;
import com.chinathinks.core.web.CommonActionForm;


/**
 * 
 * @author Dove
 *
 */
public class TemplateAction extends BizAction {
	
	
	protected Dao g4Dao= (Dao) super.getService("g4Dao");


	public ActionForward templateInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("template");
	}
	
	
	public ActionForward queryTemplate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("SmsGroup.queryTemplate", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("SmsGroup.countTemplate", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	
	
	public ActionForward removeTemplate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		
		System.out.println("dfdfdfdfdf     "+dto.get("id"));
		g4Dao.delete("SmsGroup.removeTemplate", dto);
		
		Dto sumDto  = new BaseDto();
		sumDto.put("msg", "操作成功！");
		sumDto.put("success", new Boolean(true));
		
		
		String jsonString = JsonHelper.encodeObject2Json(sumDto);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	
	public ActionForward updateTemplate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		g4Dao.delete("SmsGroup.updateTemplate", dto);
		
		Dto sumDto  = new BaseDto();
		sumDto.put("msg", "操作成功！");
		sumDto.put("success", new Boolean(true));
		
		
		String jsonString = JsonHelper.encodeObject2Json(sumDto);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	
	public ActionForward addTemplate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		g4Dao.delete("SmsGroup.addTemplate", dto);
		
		Dto sumDto  = new BaseDto();
		sumDto.put("msg", "操作成功！");
		sumDto.put("success", new Boolean(true));
		
		
		String jsonString = JsonHelper.encodeObject2Json(sumDto);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
}
